<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>添加用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <script type="text/javascript" src="../../layuiadmin/js/system/jquery.min.js"></script>
  <script src="../../layuiadmin/js/system/jquery.cookie.js"></script>
  <script type="text/javascript" src="../../layuiadmin/layui/layui.js" charset="utf-8"></script>
  <style>
    font{
      font-size: 16px;
      color: red;
    }
  </style>
  <script>
    $(function () {
      var tableuserId = $.cookie("tableUserId");
      var role = $("#roleName");
      var hrName = $("#hrName");
      $.post("http://localhost:8080/user/listRole",
              function (msg) {
                for (var i = 0; i < msg.length; i++) {
                  var option = $("<option value=" + msg[i].roleId + ">" + msg[i].roleName + "</option>");
                  role.append(option);
                  layui.use('form', function () {
                    var form = layui.form;
                    form.render("select");
                  });
                }
              })
      $.post("http://localhost:8080/user/getByName",
              function (msg) {
                for (var i = 0; i < msg.length; i++) {
                  var option = $("<option value=" + msg[i].empId + ">" + msg[i].empName + "</option>");
                  hrName.append(option);
                  layui.use('form', function () {
                    var form = layui.form;
                    form.render("select");
                  });
                }
              })
      //返回按钮
      $("#return").click(function () {
        window.location.href = "userList.html";
      });

      $("#sub").click(function () {
        var userName = $("#userName").val();
        var roleName = $("#roleName").val();
        var hrName = $("#hrName").val();
        var password = $("#password").val();
        var repasswprd = $("#repassword").val();
        var status=false;
        if(status==false) {
          if (userName == null || userName == '') {
            $("#fuserName").html("用户名不能为空，请输入用户名");
          } else if (password == '' || password == null) {
            $("#fuserName").html("");
            $("#fpass").html("密码不能为空，请输入密码");
          } else if (password != repasswprd) {
            $("#fpass").html("");
            $("#frepass").html("两次密码不一致，请重新输入");
          } else if (roleName == -1) {
            $("#frepass").html("");
            $("#froleName").html("请选择角色");
          } else if (hrName == -1) {
            $("#froleName").html("");
            $("#fhrName").html("请选择真实姓名")
          } else {
            $("#fhrName").html("");
            $.post("http://localhost:8080/user/insUserName",
                    {"userName": userName}
                    , function (msg) {
                      if (msg == 200) {
                        $("#fuserName").html("用户已存在，请重新输入用户名");
                        status = false;
                      } else {
                        $("#fuserName").html("");
                        status = true;
                      }
                    });
            $.post("http://localhost:8080/user/insUserEmp",
                    {"hrName": hrName}
                    , function (msg) {
                      if (msg == 200) {
                        $("#fhrName").html("该员工已经拥有账号，请重新选择");
                        status = false;
                      } else {
                        if(status){
                          $.post("http://localhost:8080/user/insUser",
                                  {
                                    "userName": userName,
                                    "roleName": roleName,
                                    "hrName": hrName,
                                    "password": password
                                  }
                                  , function () {
                                    window.location.href = "userList.html";
                                  })
                        }
                      }
                    });
          }
          }
        })
      })
  </script>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-row">
    <form class="layui-form" id="from">
      <div class="layui-form-item">
        <label for="username" class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
          <input type="text" id="userName" name="userName"  autocomplete="off" class="layui-input">
        </div>
        <font id="fuserName"></font>
      </div>
      <div class="layui-form-item">
      <label class="layui-form-label">新密码</label>
      <div class="layui-input-inline">
        <input type="password" name="password" id="password"  class="layui-input">
      </div><font id="fpass"></font>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">确认新密码</label>
    <div class="layui-input-inline">
      <input type="password" name="repassword" id="repassword" l autocomplete="off" class="layui-input">
    </div><font id="frepass"></font>
  </div>
      <div class="layui-form-item">
        <label for="roleName" class="layui-form-label">
          角色名称:</label>
        <div class="layui-input-inline">
          <select id="roleName" name="roleName" class="valid">
            <option value="-1"></option>
          </select>

        </div><font id="froleName"></font>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">员工名称:</label>
        <div class="layui-input-inline">
          <select  id="hrName">
            <option value="-1"></option>
          </select>
        </div><font id="fhrName"></font>
      </div>
      <div>
      <button class="layui-btn layui-btn-sm" id="sub" type="button"  lay-submit="" >添加</button>
      <button class="layui-btn layui-btn-sm" type="button"  id="return">取消</button>
      </div>
    </form>
  </div>
</div>
</body>
</html>